/*
 * @Author: your name
 * @Date: 2021-08-06 09:26:43
 * @LastEditTime: 2021-09-03 14:48:47
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \jinfeng_up\src\utils\fileDownload.js
 */
import axios from 'axios'

export default {
    fileDownload(_method, _url, _params) {
        axios({
            method: _method,
            data: _params,
            url: process.env.VUE_APP_BASE_API + _url,
            responseType: 'blob',
            headers: {
                token: JSON.parse(sessionStorage.getItem('USERINFO')).token
            }
        })
            .then((res) => {
                const objectUrl = URL.createObjectURL(res.data)
                const a = document.createElement('a')
                document.body.appendChild(a)
                a.href = objectUrl
                const filenames = decodeURIComponent(res.headers['filename'])
                a.download = filenames
                a.click()
                window.URL.revokeObjectURL(objectUrl)
            })
            .catch((err) => {
                console.log(err)
            })
    }
}

